package Market;

import java.util.ArrayList;

import sim.engine.*;


public class GenCo implements Steppable
   {
	private static final long serialVersionUID = 6628525522939315274L;


	private double aR;
	private double bR;
	private double CapRL;
	private double CapRU;
	private double[] RSupplyOffer;   // Reported supply offer (aR,bR,CapRL,CapRU), strategic
	
	private double aT; 
	private double bT; 
	private double CapTU;
	private double CapTL;
	
	private double SS = 0.05;
	private double[] TSupplyOffer;  // True supply offer (a,b,CapTL,CapTU)

	private double l;
	private double u;
	private double lR;
	private double uR;
	private double us;
	

	private ArrayList ADMat;
	private double[] alpha;
	int M;
	
											/** number of GenCos */
	private int I;

//----Constructor------------------------------------------------------------------------------------	
	public GenCo(double[][] gd, int i){
	
		aT     = gd[i][3];
		bT     = gd[i][4];
		CapTL  = gd[i][5];
		CapTU  = gd[i][6];
	}
//-----------------------------------------------------------------------------------------------------------------------------------------
    public void step(SimState state)
        {
    	 // ActionDomainConstruction();
    	 // ReportedSupplyOfferValidation(0.10, 0.20, 1.00);
    	TSupplyOffer = new double[4];
    	
    	TSupplyOffer[0]=aT;
    	TSupplyOffer[1]=bT;
    	TSupplyOffer[2]=CapTL;
    	TSupplyOffer[3]=CapTU;
        	
    	SubmitTrueSupplyOffer();
    	
    	//for (int k=0;k<4; k++){
    	//System.out.println(TSupplyOffer[k]);
    	//}
        }
    
//------------------------------------------------------------------------------------------------------------------------------------------    
/** The function checks whether reported supply offer is feasible or not 
 * @see <f/> DynTestAMES.JSLT.pdf (pg 34)  */    
    private void ReportedSupplyOfferValidation(double x, double y, double z) {
    	
    	 double RCapL;
    	 double RCapU;
    	 double RIL;
    	 double RIU;
    	
    	RIL = x;
    	RIU = y;
    	RCapU = z;
    	RCapL = CapTL/CapTU;
    	
//Step 0
   CapRL = CapTL;
   CapRL = RCapL*CapTU;
   CapRU = RCapU*(CapTU-CapTL)+CapTL;  

   l = aT+2*bT*CapTL;
   u = aT+2*bT*CapRU;

//Step 1
   lR = l/(1-RIL);
   
//Step2
   if(u > lR) { us = u; }
	   
     else {us = lR+SS;}
	  
//Step3
   uR = us/(1-RIU);
   
//Step4
   bR = 0.5*(uR-lR)/(CapRU-CapRL);
   
//Step5
   aR = lR-2*bR*CapTL;
   
   System.out.println("a_reported = " + aR);
   System.out.println("b_reported = " + bR);
    }
    
//------------------------------------------------------------------------------------------------------------------------------------------    

private void ActionDomainConstruction(){
	
	int M1=5;
    int M2=3;
    int M3=1;
    
    ADMat = new ArrayList();
    
    M = M1 * M2 * M3;

	double RIMaxL = 0.4;
	double RIMaxU = 0.4;
	double RIMinC = 1;
	
	
	double Inc1 = RIMaxL/(M1 - 1);
	double Inc2 = RIMaxU/(M2 - 1);
	double Inc3 = (1-RIMinC)/(M3-1);
	
	for (double i = 0; i <= RIMaxL; i = i+Inc1){
		for (double j = 0; j <= RIMaxU; j = j+Inc2 ){
			for(double k = 1-RIMinC; k >= 0; k = k-Inc3){  
				
	            alpha = new double[3]; // initialize triplet to zeros
	            alpha[0] = i; 
	            alpha[1] = j; 
	            alpha[2] = k +RIMinC ; 
    
	            
	            ADMat.add(alpha);
	            
	            System.out.println(alpha[0] + "   " + alpha[1] + "   " + alpha[2]);
	            
		
			}		
    	}
	}
}

public double[] SubmitReportedSupplyOffer(){

	RSupplyOffer = new double[4];
	
		RSupplyOffer[0] = aR;
		RSupplyOffer[1] = bR;
		RSupplyOffer[2] = CapRL;
		RSupplyOffer[3] = CapRU;
	
    return RSupplyOffer;
  }
    

public double[] SubmitTrueSupplyOffer(){

	//System.out.println(TSupplyOffer[1]);
    return TSupplyOffer; 
  }

	
   } 
   














